using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Promike.site.Models;

namespace Promike.site.Controllers
{
    public class PhotoAlbumController : Controller
    {
        private readonly IPhotoAlbumRepository photoalbumRepository;

        // If you are using Dependency Injection, you can delete the following constructor
        public PhotoAlbumController()
            : this(new PhotoAlbumRepository())
        {
        }

        public PhotoAlbumController(IPhotoAlbumRepository photoalbumRepository)
        {
            this.photoalbumRepository = photoalbumRepository;
        }

        [Authorize(Users = "Admin")]
        public ViewResult Index()
        {
            return View(photoalbumRepository.AllIncluding(photoalbum => photoalbum.Photos));
        }

        public ViewResult All()
        {
            return View("PhotoAlbums",photoalbumRepository.AllIncluding(photoalbum => photoalbum.Photos));
        }

        [Authorize(Users = "Admin")]
        public ViewResult Details(int id)
        {
            return View(photoalbumRepository.Find(id));
        }

        [Authorize(Users = "Admin")]
        public ActionResult Create()
        {
            return View();
        }

        [Authorize(Users = "Admin")]
        [HttpPost]
        public ActionResult Create(PhotoAlbum photoalbum)
        {
            if (ModelState.IsValid)
            {
                photoalbum.Author = User.Identity.Name;
                photoalbum.DatePublish = DateTime.Now;
                photoalbumRepository.InsertOrUpdate(photoalbum);
                photoalbumRepository.Save();
                return RedirectToAction("Index");
            }
            return View();
        }

        [Authorize(Users = "Admin")]
        public ActionResult Edit(int id)
        {
            return View(photoalbumRepository.Find(id));
        }

        [Authorize(Users = "Admin")]
        [HttpPost]
        public ActionResult Edit(PhotoAlbum photoalbum)
        {
            if (ModelState.IsValid)
            {
                photoalbumRepository.InsertOrUpdate(photoalbum);
                photoalbumRepository.Save();
                return RedirectToAction("Index");
            }
            return View();
        }

        [Authorize(Users = "Admin")]
        public ActionResult Delete(int id)
        {
            return View(photoalbumRepository.Find(id));
        }

        [Authorize(Users = "Admin")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            photoalbumRepository.Delete(id);
            photoalbumRepository.Save();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                photoalbumRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

